package service

import (
	"crypto/md5"
	"encoding/hex"
	"strings"
	"time"
)

func TimestampToDate(timestamp interface{}) (date string) {
	tm := timestamp.(int64)
	date = time.Unix(tm, 0).Format("2006-01-02 15:04:05")
	return
}
func GetContentType(fileName string) (extension string, contentType string) {
	arr := strings.Split(fileName, ".")
	// see: https://tool.oschina.net/commons/
	if len(arr) >= 2 {
		extension = arr[len(arr)-1]
		switch extension {
		case "jpeg", "jpe", "jpg":
			contentType = "image/jpeg"
		case "png":
			contentType = "image/png"
		case "gif":
			contentType = "image/gif"
		case "mp4":
			contentType = "video/mpeg4"
		case "mp3", "m4a":
			contentType = "audio/mp3"
		case "wav":
			contentType = "audio/wav"
		case "pdf":
			contentType = "application/pdf"
		case "doc", "":
			contentType = "application/msword"
		default:
			contentType = "application/octet-stream"
		}
	}
	// .*（ 二进制流，不知道下载文件类型）
	return
}
func Md5(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))
}
